package org.yylnb.openplatform.search.utils;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Map;

/**
 * @author RainLin
 * @date 2020/8/1 - 11:20
 */
public class SearchUtil {

    public static void buildMapping(CreateIndexRequest createIndexRequest, String type) throws IOException {
        XContentBuilder builder = JsonXContent.contentBuilder().startObject()//
                .startObject("properties")//
                .startObject("app_key").field("type", "keyword").endObject()//
                .startObject("apiName").field("type", "keyword").endObject()//
                .startObject("remoteIP").field("type", "ip").endObject()//
                .startObject("serverIp").field("type", "ip").endObject()//
                .startObject("receiveTime").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss").endObject()//
                .startObject("responseTime").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss").endObject()//
                .startObject("totleTime").field("type", "long").endObject()//
                .startObject("content").field("type", "text").field("analyzer", "ik_max_word").endObject()//
                .endObject()
                .endObject();
        createIndexRequest.mapping(type, builder);

    }

    /**
     * 根据传递的参数的map来返回一个带有查询条件的builder
     *
     * @param map
     * @return
     */
    public static SearchSourceBuilder getSearchSourceBuilder(Map map) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        String requestContent = (String) map.get("requestContent");//查询的关键字
        //我们期望的条件可能会有很多
        BoolQueryBuilder booleanQueryBuilder = null;

        if (!StringUtils.isEmpty(requestContent)) {
            booleanQueryBuilder = booleanQueryBuilder == null ? QueryBuilders.boolQuery() : booleanQueryBuilder;
            booleanQueryBuilder.must(QueryBuilders.matchQuery("content", requestContent));//要求content中必须包含指定的条件
        }

        String app_key = (String) map.get("app_key");

        if (!StringUtils.isEmpty(app_key)) {
            booleanQueryBuilder = booleanQueryBuilder == null ? QueryBuilders.boolQuery() : booleanQueryBuilder;
            booleanQueryBuilder.must(QueryBuilders.termQuery("app_key", app_key));
        }

        String apiName = (String) map.get("apiName");

        if (!StringUtils.isEmpty(apiName)) {
            booleanQueryBuilder = booleanQueryBuilder == null ? QueryBuilders.boolQuery() : booleanQueryBuilder;
            booleanQueryBuilder.must(QueryBuilders.termQuery("apiName", apiName));
        }
        String remoteIP = (String) map.get("remoteIP");

        if (!StringUtils.isEmpty(remoteIP)) {
            booleanQueryBuilder = booleanQueryBuilder == null ? QueryBuilders.boolQuery() : booleanQueryBuilder;
            booleanQueryBuilder.must(QueryBuilders.termQuery("remoteIP", remoteIP));
        }

        String serverIp = (String) map.get("serverIp");

        if (!StringUtils.isEmpty(serverIp)) {
            booleanQueryBuilder = booleanQueryBuilder == null ? QueryBuilders.boolQuery() : booleanQueryBuilder;
            booleanQueryBuilder.must(QueryBuilders.termQuery("serverIp", serverIp));
        }


        String receiveTime = (String) map.get("startTime");

        if (!StringUtils.isEmpty(receiveTime)) {
            booleanQueryBuilder = booleanQueryBuilder == null ? QueryBuilders.boolQuery() : booleanQueryBuilder;
            booleanQueryBuilder.must(QueryBuilders.rangeQuery("receiveTime").gte(receiveTime));
        }


        String endTime = (String) map.get("endTime");

        if (!StringUtils.isEmpty(endTime)) {
            booleanQueryBuilder = booleanQueryBuilder == null ? QueryBuilders.boolQuery() : booleanQueryBuilder;
            booleanQueryBuilder.must(QueryBuilders.rangeQuery("receiveTime").lte(endTime));
        }

        if (booleanQueryBuilder != null) {
            searchSourceBuilder.query(booleanQueryBuilder);
        }
        return searchSourceBuilder;

    }
}

